//
//  SettingViewController.m
//  Shanghai Cuisine
//
//  Created by iOS Two on 14/12/18.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "SettingViewController.h"
#import "FavoriteViewController.h"
#import "AppDelegate.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatController];
    UILabel *titelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 44)];
    titelLabel.text = @"我的";
    titelLabel.textAlignment = NSTextAlignmentCenter;
    titelLabel.font = [UIFont systemFontOfSize:19];
    titelLabel.textColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationItem.titleView = titelLabel;
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.9 green:0.64 blue:0.16 alpha:1];    // Do any additional setup after loading the view.
}
-(void)creatController {
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight) style:UITableViewStylePlain];
    _mainTable.delegate = self;
    _mainTable.dataSource = self;
    _mainTable.rowHeight = 80;
    _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    _mainTable.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [self.view addSubview:_mainTable];
    
    UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kwidth, 20)];
    back.backgroundColor = [UIColor clearColor];
    _mainTable.tableFooterView = back;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, kwidth, 56)];
    image.image = [UIImage imageNamed:[NSString stringWithFormat:@"shezhi_0%d",indexPath.row+1]];
    image.layer.shadowColor = [UIColor grayColor].CGColor;
    image.layer.shadowOffset = CGSizeMake(2, 2);
    image.layer.shadowOpacity = 0.5;
    image.layer.shadowRadius = 1.0;
    [cell.contentView addSubview:image];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        FavoriteViewController *favorite = [[FavoriteViewController alloc]init];
        self.navigationItem.title = @"返回";
        [self.navigationController pushViewController:favorite animated:YES];
    } else if (indexPath.row == 1) {
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];
        NSString *URL = @"http://itunes.apple.com/lookup?id=920317079";
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:URL]];
        [request setHTTPMethod:@"POST"];
        NSHTTPURLResponse *urlResponse = nil;
        NSError *error = nil;
        NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
        NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];
        NSDictionary *dic = [results JSONValue];
        NSArray *infoArray = [dic objectForKey:@"results"];
        if ([infoArray count]) {
            NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
            NSString *lastVersion = [releaseInfo objectForKey:@"version"];
            if (![lastVersion isEqualToString:currentVersion]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新，是否前往更新？" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];
                alert.tag = 1;
                [alert show];
            } else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本"delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alert.tag = 2;
                [alert show];
            }
        }
    } else if (indexPath.row == 2) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"是否清除缓存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 3;
        [alert show];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1) {
        if (buttonIndex == 1) {
            NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/shang-hai-ben-bang-cai/id920317079?mt=8"];
            [[UIApplication sharedApplication]openURL:url];
        }
    } else if (alertView.tag == 3) {
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        [appDelegate.imageCache clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
        [AVQuery clearAllCachedResults];
        [[SDImageCache sharedImageCache] clearDisk];
        [[SDImageCache sharedImageCache] clearMemory];
        [self alertViewShow:@"清除成功"];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
